package com.lu.aio.client;

import cn.hutool.core.util.CharsetUtil;
import com.lu.GlobalConfig;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @program lu-netty
 * @description:
 * @author: zhanglu
 * @create: 2022-06-10 17:45:00
 */
@Slf4j
public class AioClient {

    public static void main(String[] args) throws Exception {
        AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
        //连接服务端
        socketChannel.connect(new InetSocketAddress(GlobalConfig.LOCAL_IP, GlobalConfig.LOCAL_PORT));
        log.info("AioClient start...");
        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        //创建事件处理器
        socketChannel.read(buffer, buffer, new AioClientHandler(socketChannel, CharsetUtil.UTF_8));
        Thread.currentThread().join();
    }

    public void s() throws Exception {
        AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
        socketChannel.connect(new InetSocketAddress(GlobalConfig.LOCAL_IP, GlobalConfig.LOCAL_PORT), this, new CompletionHandler<Void, AioClient>() {
            @Override
            public void completed(Void result, AioClient attachment) {
                byte[] req = "hello lu".getBytes();
                ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
                writeBuffer.put(req);
                writeBuffer.flip();
                socketChannel.write(writeBuffer, writeBuffer, new CompletionHandler<Integer, Object>() {
                    @Override
                    public void completed(Integer result, Object obj) {
                        ByteBuffer buffer = (ByteBuffer) obj;
                        if(buffer.hasRemaining()){
                            socketChannel.write(buffer, buffer, this);
                        }else {
                            ByteBuffer readBuffer = ByteBuffer.allocate(1024);
                            socketChannel.read(readBuffer, readBuffer, new CompletionHandler<Integer, Object>() {
                                @Override
                                public void completed(Integer result, Object obj) {
                                    ByteBuffer buffer = (ByteBuffer) obj;
                                    buffer.flip();
                                    byte[] bytes = new byte[buffer.remaining()];
                                    buffer.get(bytes);
                                    try {
                                        String body = new String(bytes, CharsetUtil.UTF_8);
                                        log.info(body);
                                    } catch (UnsupportedEncodingException e) {
                                        e.printStackTrace();
                                    }

                                }
                                @Override
                                public void failed(Throwable exc, Object attachment) {
                                }
                            });
                        }
                    }
                    @Override
                    public void failed(Throwable exc, Object attachment) {

                    }
                });

            }

            @Override
            public void failed(Throwable exc, AioClient attachment) {

            }
        });
        log.info("AioClient start...");
        Thread.currentThread().join();
    }

    public static void main2(String[] args) throws Exception {
        new AioClient().s();
    }

}
